package com.sais.manage.boot.platform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.platform.annotation.Throttle;
import com.sais.manage.boot.platform.dto.MenuDTO;
import com.sais.manage.boot.platform.dto.MenuPermissionDTO;
import com.sais.manage.boot.platform.dto.MenuSaveDTO;
import com.sais.manage.boot.platform.dto.MenuSearchDTO;
import com.sais.manage.boot.platform.dto.MenuTreeDTO;
import com.sais.manage.boot.platform.dto.UserLoginDTO;
import com.sais.manage.boot.platform.service.IMenuService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 宿舍管理系统-菜单表 前端控制器
 *
 * @author zhang
 * @date 2019/11/28
 **/
@Api(tags = "菜单管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/platform/menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @ApiOperation("保存 菜单")
//    @Idempotent
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.saveMenu(menuSaveDTO);
        return new Result();
    }

    @ApiOperation("新建 菜单")
    //@Idempotent
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.createMenu(menuSaveDTO);
        return new Result();
    }

    @ApiOperation("删除 菜单")
    @Throttle
    @RequestMapping(value = "/delete/{ukId}", method = RequestMethod.GET)
    public Result deleteMenu(@PathVariable("ukId") String menuId) {
        menuService.deleteMenu(menuId);
        return new Result();
    }

    @ApiOperation("修改 菜单")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateMenu(@RequestBody MenuSaveDTO menuSaveDTO) {
        menuService.updateMenu(menuSaveDTO);
        return new Result();
    }

    @ApiOperation("查询 菜单")
    @Throttle
    @RequestMapping(value = "/select/{ukId}", method = RequestMethod.GET)
    public Result selectMenu(@PathVariable("ukId") String menuId) {
        MenuDTO menuDTO = menuService.selectMenu(menuId);
        return new Result().data("menu", menuDTO);
    }

    @ApiOperation("查询 菜单 列表")
    @Throttle
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listMenu(MenuSearchDTO searchDTO,
                           @RequestParam(name = "current", defaultValue = "1") Integer current,
                           @RequestParam(name = "size", defaultValue = "30") Integer size) {
        Page<MenuDTO> menus = menuService.listMenu(searchDTO, current, size);
        return new Result().data("menus", menus);
    }

    @ApiOperation("查询 菜单树 根据 父id ")
    @Throttle
    @RequestMapping(value = "/list/tree/{parentId}", method = RequestMethod.GET)
    public Result listMenuTree(@PathVariable("parentId") String parentId) {
        List<MenuTreeDTO> menuTree = menuService.listMenuTree(parentId);
        return new Result().data("menuTree", menuTree);
    }

    @ApiOperation("菜单 绑定权限")
    @Throttle
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result bindMenuPermission(@RequestBody MenuPermissionDTO menuPermissionDTO) {
        menuService.bindMenuPermission(menuPermissionDTO.getMenuId(), menuPermissionDTO.getPermissionIds());
        return new Result();
    }

    @ApiOperation(value = "根据用户id 查询 用户 系统 列表")
    @Throttle
    @RequestMapping(value = "list/system/user/{userId}", method = RequestMethod.GET)
    public Result listSystemByUserId(@PathVariable(value = "userId") String userId) {
        List<MenuDTO> menus = menuService.listSystemByUserId(userId);
        return Result.response().data("menus", menus);
    }

    @ApiOperation(value = "查询 指定系统菜单树 根据当前登录用户 ")
    @Throttle
    @RequestMapping(value = "list/tree/system/{systemId}", method = RequestMethod.GET)
    public Result listMenuTreeByUser(@PathVariable(value = "systemId") String systemId) {
        UserLoginDTO userLoginDTO = ShiroUtils.getLoginUser();
        List<MenuTreeDTO> menuTrees = menuService.listMenuTreeByUser(systemId, userLoginDTO.getUkId());
        return Result.response().data("menuTrees", menuTrees);
    }

    @ApiOperation(value = "查询 当前用户下 所有菜单级别为0 的菜单树")
    @RequestMapping(value = "/list/user/{userId}", method = RequestMethod.GET)
    public Result listMenuByUser(@PathVariable(value = "userId") String userId) {
        List<MenuTreeDTO> menuTrees = menuService.listMenuByUser(userId);
        return new Result().data("menus", menuTrees);
    }


}
